<!-- 会员管理 -->
<template>
  <div class="box_container">
    <!-- 输入框 -->
    <div class="box_input">
      <el-select v-model="pageinfo.status" placeholder="请选择用户状态">
        <el-option v-for="item in statusDic" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-select v-model="pageinfo.level" placeholder="请选择用户会员等级">
        <el-option v-for="item in levelDic" :key="item.value1" :label="item.label" :value="item.value1">
        </el-option>
      </el-select>
      <el-select v-model="pageinfo.gender" placeholder="请选择用户性别">
        <el-option v-for="item in genderDic" :key="item.value2" :label="item.label" :value="item.value2">
        </el-option>
      </el-select>
      <el-input v-model="pageinfo.id" placeholder="请输入用户Id"></el-input>
      <el-input v-model="pageinfo.nickname" placeholder="请输入昵称"></el-input>
      <el-button icon="el-icon-search" type="primary" @click="onSeek">查找</el-button>
    </div>
    <!-- 会员管理列表 -->
    <div class="box_table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="用户ID" align="center">
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="手机号码" align="center">
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">
            <el-tag> {{scope.row.gender == "1"? "女": "男"}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="会员等级" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.level==0?'info':'warning'"> {{scope.row.level == "0"? "普通会员": "VIP会员"}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="birthday" label="生日" align="center">
          <template slot-scope="scope">
            {{new Date(scope.row.gmtCreate).format()}}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status==0?'danger':'primary'"> {{scope.row.status == "0"? "冻结": "激活"}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="gmtLastLogin" label="上次登录时间" align="center">
          <template slot-scope="scope">
            {{new Date(scope.row.gmtCreate).couponTime()}}
          </template>
        </el-table-column>
        <el-table-column prop="lastLoginIp" label="上次登录IP" align="center">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="editStatus(scope.row)">{{scope.row.status == 1? "冻结": "激活"}}</el-button>
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑弹框 -->
    <el-dialog title="编辑" :visible.sync="dialogTableVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="gender">
          <el-select v-model="ruleForm.gender" placeholder="请选择">
            <el-option v-for="item in genderDic" :key="item.value2" :label="item.label" :value="item.value2">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户等级" prop="level">
          <el-select v-model="ruleForm.level" placeholder="请选择">
            <el-option v-for="item in levelDic" :key="item.value1" :label="item.label" :value="item.value1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态" prop="status">
          <el-select v-model="ruleForm.status" placeholder="请选择">
            <el-option v-for="item in statusDic" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="display: flex;justify-content: end;">
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分页 --回到顶部 -->
    <div class="box_paging">
      <pagination :pageinfo="pageinfo" @change="changePage"></pagination>
    </div>
  </div>
</template>
<script>
import Pagination from "../../components/pagination/MyPagination.vue"
import Apiadmin from "../../api/admin"
import { debounce } from '../../utils/debounce'
export default {
  components: { Pagination },
  data() {
    return {
      // loading:false,
      pageinfo: {
        limit: 10, //每页显示的数量
        page: 1,  //当前显示的页码
        total: 0, //数据总条数
        id: '', //用户id
        nickname: '', //用户昵称
        level: null,//用户等级
        gender: null, //用户性别
        status: null,  //用户状态
      },
      tableData: [], //表格数据
      //用户状态选项
      statusDic: [{
        value: 0,
        label: '冻结'
      }, {
        value: 1,
        label: '激活'
      }, {
        value: '',
        label: '全部'
      },],
      value: '',
      //用户会员等级选项
      levelDic: [{
        value1: 0,
        label: '普通会员'
      }, {
        value1: 1,
        label: 'VIP会员'
      }, {
        value1: '',
        label: '全部'
      },],
      value1: '',
      //用户性别选项
      genderDic: [{
        value2: 1,
        label: '女'
      }, {
        value2: 2,
        label: '男'
      }, {
        value2: '',
        label: '全部'
      },],
      value2: '',
      // 表单数据
      ruleForm: {
        nickname: '', //用户昵称
        phone: '',//用户手机号
        gender: '', //用户性别
        level: '',//用户等级
        status: '',  //用户状态
      },
      // 表单验证
      rules: {
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^0?(13|14|15|17|18|19)[0-9]{9}$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,  //编辑弹框隐藏
    }
  },
  created() {
    this.operateList()
  },
  methods: {
    // 请求会员管理数据
    operateList() {
      Apiadmin.userList({ ...this.pageinfo, pageNo: this.pageinfo.page }).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.tableData = res.data.items  //表格数据
          this.pageinfo.total = res.data.count  //分页总数据
        }
      })
    },
    //查找会员用户
    onSeek() {
      this.pageinfo.page = 1,  //查找页数重置为1
        // 请求数据时加载
        this.$showLoading(() => {
          this.operateList()
        })
    },
    // 激活冻结
    editStatus: debounce(function (row) {   //防抖操作.避免连续点击
      //  console.log(this)
      let status = row.status ? 0 : 1;
      Apiadmin.userStatus({ userId: row.id, status }).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.$message({
            message: status == 0 ? '冻结成功' : '激活成功',
            type: status == 0 ? 'error' : 'success'
          });
          this.operateList()   //刷新页面
        }
      })
    }, 1000),
    // 编辑
    handleEdit(row) {
      // console.log(row)
      this.dialogTableVisible = true  //编辑弹框显示
      this.ruleForm = row
      // 会修改同一个地址的值，所以需要深度拷贝一下
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.row = JSON.parse(JSON.stringify(row));
      this.$nextTick(() => { // 延迟回调,避免未找到重置resetFields的方法
        this.$refs['ruleForm'].resetFields(); // 重置
      })
    },
    //编辑提交按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          Apiadmin.userEdit(JSON.stringify(this.ruleForm)).then(res => {
            // console.log(res)
            if (res.errno == 200) {
              this.dialogTableVisible = false  //关闭弹框
              this.operateList()   //刷新页面
              this.$message({
                message: '修改成功',
                type: 'success'
              });
            } else {
              this.$message({
                message: "修改失败",
                type: 'error'
              });
            }
          }).catch(err => {
            console.log(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //取消按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogTableVisible = false  //编辑弹框关闭
    },
    // 子组件传过来的方法和页长跟当前页
    changePage(e) {
      console.log(e)
      this.pageinfo = JSON.parse(JSON.stringify(e))
      this.$nextTick(() => {
        this.operateList()
      })
    },

  }
}
</script>
<style scoped  lang="less">
.box_input {
  .el-input {
    width: 15%;
  }
  // .el-select {
  //   margin-right: 10px;
  // }
}
</style>